package com.yssw.news.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yssw.common.annotation.Anonymous;
import com.yssw.model.vo.news.NewsTypeVo;
import com.yssw.model.vo.news.NewsVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yssw.common.annotation.Log;
import com.yssw.common.core.controller.BaseController;
import com.yssw.common.core.domain.AjaxResult;
import com.yssw.common.enums.BusinessType;
import com.yssw.news.domain.NewsTable;
import com.yssw.news.service.INewsTableService;
import com.yssw.common.utils.poi.ExcelUtil;
import com.yssw.common.core.page.TableDataInfo;

/**
 * 新闻Controller
 *
 * @author yssw
 * @date 2023-03-27
 */
@Anonymous
@RestController
@RequestMapping("/news/news")
public class NewsTableController extends BaseController {
    @Autowired
    private INewsTableService newsTableService;

    /**
     * 查询新闻列表
     */
//    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(NewsTable newsTable) {
        startPage();
        List<NewsTable> list = newsTableService.selectNewsTableList(newsTable);
        return getDataTable(list);
    }

    /**
     * 查询新闻类型列表
     */
//    @PreAuthorize("@ss.hasPermi('news:news:list')")
    @GetMapping("/listType")
    public AjaxResult listType() {
        return AjaxResult.success(newsTableService.selectNewsTypeList());
    }

    /**
     * 导出新闻列表
     */
//    @PreAuthorize("@ss.hasPermi('news:news:export')")
    @Log(title = "新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NewsTable newsTable) {
        List<NewsTable> list = newsTableService.selectNewsTableList(newsTable);
        ExcelUtil<NewsTable> util = new ExcelUtil<NewsTable>(NewsTable.class);
        util.exportExcel(response, list, "新闻数据");
    }

    /**
     * 获取新闻详细信息
     */
//    @PreAuthorize("@ss.hasPermi('news:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(newsTableService.selectNewsTableById(id));
    }

    /**
     * 新增新闻
     */
//    @PreAuthorize("@ss.hasPermi('news:news:add')")
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NewsTable newsTable) {
        return toAjax(newsTableService.insertNewsTable(newsTable));
    }

    /**
     * 修改新闻
     */
//    @PreAuthorize("@ss.hasPermi('news:news:edit')")
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NewsTable newsTable) {
        return toAjax(newsTableService.updateNewsTable(newsTable));
    }

    /**
     * 删除新闻
     */
//    @PreAuthorize("@ss.hasPermi('news:news:remove')")
    @Log(title = "新闻", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(newsTableService.deleteNewsTableByIds(ids));
    }


    /**
     * 删除新闻类型
     */
    @PreAuthorize("@ss.hasPermi('news:news:remove')")
    @Log(title = "新闻类型", businessType = BusinessType.DELETE)
    @DeleteMapping("type/{ids}")
    public AjaxResult removeType(@PathVariable Long[] ids) {
        return toAjax(newsTableService.deleteNewsTypeByIds(ids));
    }
    /**
     * 查询新闻类型
     */
    @GetMapping("/typeList")
    public TableDataInfo queryType(@PathVariable Long typeId) {
        NewsVo newsVo = newsTableService.selectNewsTableById(typeId);
        ArrayList<Object> result = new ArrayList<>();
        result.add(newsVo);
        return getDataTable(result);
    }
    /**
     * 修改新闻类型
     */
    @Log(title = "新闻类型", businessType = BusinessType.UPDATE)
    @PutMapping("/type")
    public AjaxResult updateType(@PathVariable NewsTypeVo newsTypeVo) {
        return toAjax(newsTableService.updateNewsType(newsTypeVo));
    }
    /**
     * 添加新闻类型
     */
    @Log(title = "新闻类型", businessType = BusinessType.INSERT)
    @PostMapping("/type")
    public AjaxResult addType(@PathVariable NewsTypeVo newsTypeVo) {
        return toAjax(newsTableService.insertNewsType(newsTypeVo));
    }


}
